﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyProj.MVCProj3.DAL;
using MyProj.MVCProj3.IDAL;
using MyProj.MVCProj3.Model;

namespace MyProj.MVCProj3.BLL
{
    public abstract class BaseService<T>
    {
        public IBaseRepository<T> CurrentRepository { get; set; }
        //抽象方法，由子类决定
        public abstract void SetCurrentRepository();
        //在构造函数中调用抽象方法，要求子类必须实现，注意构造函数不需要写泛型T？
        public BaseService()
        {
            SetCurrentRepository();
        }
        protected IDbSession dbSession = DbSessionFactroy.GetCurrentDbSession();

        public IQueryable<T> LoadEntities(Func<T, bool> whereLamda)
        {

            return CurrentRepository.LoadEntities(whereLamda);
        }

        public IQueryable<T> LoadPageEntities<TKey>(int pageSize, int pageIndex, out int total, Func<T, bool> whereLamda, Func<T, TKey> ordeyByLamda, bool isAsc)
        {
            return CurrentRepository.LoadPageEntities<TKey>(pageSize, pageIndex, out total, whereLamda, ordeyByLamda, isAsc);
        }

        public bool AddEntity(T entity)
        {
            CurrentRepository.AddEntity(entity);
            return dbSession.SaveChanges() > 0;
        }

        public bool DeleteEntity(T entity)
        {
            CurrentRepository.DeleteEntity(entity);
            return dbSession.SaveChanges() > 0;
        }

        public bool UpdateEntity(T entity)
        {
            CurrentRepository.UpdateEntity(entity);
            return dbSession.SaveChanges()>0;
        }
    }

    public class UserInfoService : BaseService<UserInfo> 
    {
        public override void SetCurrentRepository()
        {
            CurrentRepository = dbSession.UserInfo;
        }
    }

}
